﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Konst3d.Efficiency.Suite;
using Konst3d.Efficiency.Suite.SparsedBinary;
using System.Text;
using Konst3d.Efficiency;

namespace DevUI
{
    #region Types

    public class MonitorListItem
    {
        public int ID;
        public string Name;
        public IMonitor Monitor;

        public override string ToString()
        {
            double value = 0;

            switch(Monitor.Type)
            {
                case MonitorType.Monitor:
                    value = Monitor.GetAverageValue();
                    break;
                case MonitorType.Counter:
                    value = Monitor.GetSummaryValue();
                    break;
                case MonitorType.Commandment:
                    value = Monitor.GetCommandmentViolationsCount();
                    break;
            }

            return String.Format("{0} [{1}] {2}", Monitor.Title, Monitor.Type.ToString(), value);
        }
    }

    public class MilestoneListItem
    {
        public IMilestone Milestone;
        public int ID;

        public override string ToString()
        {
            return Milestone.Title + " [" + Milestone.Date.ToString(@"dd/MM/yyyy") +"]";
        }
    }

    public class TaskListItem
    {
        public ITask Task;
        public int ID;

        public override string ToString()
        {
            return Task.Title;
        }
    }

    public class CountDownListItem
    {
        public ICountDownTimer Timer;
        public int ID;

        public override string ToString()
        {
            return Timer.Title;
        }
    }

    public class MonitoringGroupListItem
    {
        public IMonitoringGroup Group;
        public int ID;

        public override string ToString()
        {
            return String.Format("{0} [{1}]", Group.Title, Group.ID);
        }
    }

    public class BuyListItem
    {
        public IBuy Buy;

        public override string ToString()
        {
            return Buy.Title;
        }
    }

    public class NoteListItem
    {
        public INote Note;

        public override string ToString()
        {
            return Note.Title;
        }
    }

    #endregion // Types

    // ability to view summary (all monitors) per day\week\month\year
    public partial class MainForm : Form
    {
        #region Fields

        private LoginForm _loginForm = new LoginForm();
        private CreateSuiteForm _createSuiteForm = new CreateSuiteForm();
        private CreateMonitorDialog _createMonitorDlg = new CreateMonitorDialog();
        private MonitorStatisticsForm _monitorStatisticDlg = new MonitorStatisticsForm();
        private DailyReportForm _reportForm = new DailyReportForm();
        private CreateMilestoneDialog _createMilesDlg = new CreateMilestoneDialog();
        private CreateTaskDialog _createTaskDlg = new CreateTaskDialog();
        private CreateCountDownDialog _createCountDownDlg = new CreateCountDownDialog();
        private CreateMonitoringGroupDialog _createMonitoringGroupDlg = new CreateMonitoringGroupDialog();
        private CreateBuyDialog _createBuyDlg = new CreateBuyDialog();
        private CreateNoteDialog _createNoteDlg = new CreateNoteDialog();

        private IEfficiencySuite _suite;
        private ICryptographicEngine _cryptographicEngine = new SimpleBinaryEncoder(Encoding.UTF8);

        #endregion // Fields

        #region Suite Management

        private void CreateSuite()
        {
            CloseSuite();

            if (_createSuiteForm.ShowDialog() == DialogResult.OK)
            {
                string login = "", password = "";

                if (_createSuiteForm.PasswordProtection)
                {
                    login = _createSuiteForm.Login;
                    password = _createSuiteForm.Password;
                }

                _suite = SparseBinaryStorage.Create(_createSuiteForm.FileName, true, login, password, _cryptographicEngine);
            }

            UpdateUI();
        }

        private void OpenSuite(string path)
        {
            string login = String.Empty;
            string password = String.Empty;

            CloseSuite();

            if (SparseBinaryStorage.IsPasswordProtected(path, _cryptographicEngine))
            {
                if (_loginForm.ShowDialog() == DialogResult.OK)
                {
                    login = _loginForm.Login;
                    password = _loginForm.Password;
                }
            }

            _suite = SparseBinaryStorage.Open(path, login, password, _cryptographicEngine);

            UpdateUI();
        }

        private void CloseSuite()
        {
            if (_suite != null)
            {
                _suite.Dispose();
                _suite = null;
            }

            ClearUI();
        }

        private void DeleteSuite()
        {
            if (_suite != null)
            {
                _suite.Delete();
                _suite = null;
            }

            ClearUI();
        }

        #endregion // Suite Management

        #region Summary Management

        private void ClearSummaryUI()
        {
            uiSummaryMonitorsCountLabel.Text = "0";
            uiSummaryMonitorsListBox.Items.Clear();
        }

        private void FillSummaryUI()
        {
            uiSummaryEfficiency.Text = _suite.GetSummaryEfficiency().ToString(".##") + "%";
            uiSummaryMonitorsCountLabel.Text = _suite.Monitors.GetActiveCount().ToString() + " active monitors.";

            foreach (var monitor in _suite.Monitors)
            {
                if (monitor.Active)
                {
                    uiSummaryMonitorsListBox.Items.Add(new MonitorListItem() { ID = monitor.ID, Name = monitor.Title, Monitor = monitor });
                }
            }
        }

        public void OpenDailyReport()
        {
            _reportForm.ShowReport(_suite);
        }

        #endregion // Summary Management

        #region Monitors Management

        private void AddMonitor()
        {
            if (_createMonitorDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int id = _suite.Monitors.Add(_createMonitorDlg.MonitorType, _createMonitorDlg.MonitorName, _createMonitorDlg.MonitorDescription, DateTime.Now, true);

                uiMonitorsListBox.Items.Add(new MonitorListItem() { ID = id, Name = _createMonitorDlg.MonitorName, Monitor = _suite.Monitors[id] });
            }
        }

        private void RemoveMonitor(int id)
        {
            _suite.Monitors.Remove(id);

            for (int i = 0; i < uiMonitorsListBox.Items.Count; ++i)
            {
                if (((MonitorListItem)uiMonitorsListBox.Items[i]).ID == id)
                {
                    uiMonitorsListBox.Items.Remove(uiMonitorsListBox.Items[i]);
                    break;
                }
            }

            if (_suite.Monitors.Count == 0)
            {
                ClearMonitorUI();
            }
        }

        private void ShowMonitorInfo(int id)
        {
            IMonitor monitor = _suite.Monitors[id];

            uiMonitorTypeTextBox.Text = monitor.Type.ToString();
            uiMonitorNameTextBox.Text = monitor.Title;
            uiMonitorDescriptionTextBox.Text = monitor.Description;
            uiMonitorCreatedDateTextBox.Text = monitor.Created.ToString();
            uiMonitorActiveCheckBox.Checked = monitor.Active;

            uiWeekDayActiveCHeckBox0.Checked = monitor.ActiveDays.DaysOfWeek[0];
            uiWeekDayActiveCHeckBox1.Checked = monitor.ActiveDays.DaysOfWeek[1];
            uiWeekDayActiveCHeckBox2.Checked = monitor.ActiveDays.DaysOfWeek[2];
            uiWeekDayActiveCHeckBox3.Checked = monitor.ActiveDays.DaysOfWeek[3];
            uiWeekDayActiveCHeckBox4.Checked = monitor.ActiveDays.DaysOfWeek[4];
            uiWeekDayActiveCHeckBox5.Checked = monitor.ActiveDays.DaysOfWeek[5];
            uiWeekDayActiveCHeckBox6.Checked = monitor.ActiveDays.DaysOfWeek[6];

            uiMonthDayActiveCHeckBox0.Checked = monitor.ActiveDays.DaysOfMonth[ 0];
            uiMonthDayActiveCHeckBox1.Checked = monitor.ActiveDays.DaysOfMonth[ 1];
            uiMonthDayActiveCHeckBox2.Checked = monitor.ActiveDays.DaysOfMonth[ 2];
            uiMonthDayActiveCHeckBox3.Checked = monitor.ActiveDays.DaysOfMonth[ 3];
            uiMonthDayActiveCHeckBox4.Checked = monitor.ActiveDays.DaysOfMonth[ 4];
            uiMonthDayActiveCHeckBox5.Checked = monitor.ActiveDays.DaysOfMonth[ 5];
            uiMonthDayActiveCHeckBox6.Checked = monitor.ActiveDays.DaysOfMonth[ 6];
            uiMonthDayActiveCHeckBox7.Checked = monitor.ActiveDays.DaysOfMonth[ 7];
            uiMonthDayActiveCHeckBox8.Checked = monitor.ActiveDays.DaysOfMonth[ 8];
            uiMonthDayActiveCHeckBox9.Checked = monitor.ActiveDays.DaysOfMonth[ 9];
            uiMonthDayActiveCHeckBox10.Checked = monitor.ActiveDays.DaysOfMonth[10];
            uiMonthDayActiveCHeckBox11.Checked = monitor.ActiveDays.DaysOfMonth[11];
            uiMonthDayActiveCHeckBox12.Checked = monitor.ActiveDays.DaysOfMonth[12];
            uiMonthDayActiveCHeckBox13.Checked = monitor.ActiveDays.DaysOfMonth[13];
            uiMonthDayActiveCHeckBox14.Checked = monitor.ActiveDays.DaysOfMonth[14];
            uiMonthDayActiveCHeckBox15.Checked = monitor.ActiveDays.DaysOfMonth[15];
            uiMonthDayActiveCHeckBox16.Checked = monitor.ActiveDays.DaysOfMonth[16];
            uiMonthDayActiveCHeckBox17.Checked = monitor.ActiveDays.DaysOfMonth[17];
            uiMonthDayActiveCHeckBox18.Checked = monitor.ActiveDays.DaysOfMonth[18];
            uiMonthDayActiveCHeckBox19.Checked = monitor.ActiveDays.DaysOfMonth[19];
            uiMonthDayActiveCHeckBox20.Checked = monitor.ActiveDays.DaysOfMonth[20];
            uiMonthDayActiveCHeckBox21.Checked = monitor.ActiveDays.DaysOfMonth[21];
            uiMonthDayActiveCHeckBox22.Checked = monitor.ActiveDays.DaysOfMonth[22];
            uiMonthDayActiveCHeckBox23.Checked = monitor.ActiveDays.DaysOfMonth[23];
            uiMonthDayActiveCHeckBox24.Checked = monitor.ActiveDays.DaysOfMonth[24];
            uiMonthDayActiveCHeckBox25.Checked = monitor.ActiveDays.DaysOfMonth[25];
            uiMonthDayActiveCHeckBox26.Checked = monitor.ActiveDays.DaysOfMonth[26];
            uiMonthDayActiveCHeckBox27.Checked = monitor.ActiveDays.DaysOfMonth[27];
            uiMonthDayActiveCHeckBox28.Checked = monitor.ActiveDays.DaysOfMonth[28];
            uiMonthDayActiveCHeckBox29.Checked = monitor.ActiveDays.DaysOfMonth[29];
            uiMonthDayActiveCHeckBox30.Checked = monitor.ActiveDays.DaysOfMonth[30];
        }

        private void ShowMonitorStatistics(int ID)
        {
            _monitorStatisticDlg.ShowStatistic(_suite.Monitors[ID]);
        }

        private void SaveMonitor(int ID)
        {
            IMonitor monitor = _suite.Monitors[ID];

            monitor.Title       = uiMonitorNameTextBox.Text;
            monitor.Description = uiMonitorDescriptionTextBox.Text;
            monitor.Active      = uiMonitorActiveCheckBox.Checked;

            monitor.ActiveDays.DaysOfWeek[0] = uiWeekDayActiveCHeckBox0.Checked;
            monitor.ActiveDays.DaysOfWeek[1] = uiWeekDayActiveCHeckBox1.Checked;
            monitor.ActiveDays.DaysOfWeek[2] = uiWeekDayActiveCHeckBox2.Checked;
            monitor.ActiveDays.DaysOfWeek[3] = uiWeekDayActiveCHeckBox3.Checked;
            monitor.ActiveDays.DaysOfWeek[4] = uiWeekDayActiveCHeckBox4.Checked;
            monitor.ActiveDays.DaysOfWeek[5] = uiWeekDayActiveCHeckBox5.Checked;
            monitor.ActiveDays.DaysOfWeek[6] = uiWeekDayActiveCHeckBox6.Checked;

            monitor.ActiveDays.DaysOfMonth[0] = uiMonthDayActiveCHeckBox0.Checked;
            monitor.ActiveDays.DaysOfMonth[1] = uiMonthDayActiveCHeckBox1.Checked;
            monitor.ActiveDays.DaysOfMonth[2] = uiMonthDayActiveCHeckBox2.Checked;
            monitor.ActiveDays.DaysOfMonth[3] = uiMonthDayActiveCHeckBox3.Checked;
            monitor.ActiveDays.DaysOfMonth[4] = uiMonthDayActiveCHeckBox4.Checked;
            monitor.ActiveDays.DaysOfMonth[5] = uiMonthDayActiveCHeckBox5.Checked;
            monitor.ActiveDays.DaysOfMonth[6] = uiMonthDayActiveCHeckBox6.Checked;
            monitor.ActiveDays.DaysOfMonth[7] = uiMonthDayActiveCHeckBox7.Checked;
            monitor.ActiveDays.DaysOfMonth[8] = uiMonthDayActiveCHeckBox8.Checked;
            monitor.ActiveDays.DaysOfMonth[9] = uiMonthDayActiveCHeckBox9.Checked;
            monitor.ActiveDays.DaysOfMonth[10] = uiMonthDayActiveCHeckBox10.Checked;
            monitor.ActiveDays.DaysOfMonth[11] = uiMonthDayActiveCHeckBox11.Checked;
            monitor.ActiveDays.DaysOfMonth[12] = uiMonthDayActiveCHeckBox12.Checked;
            monitor.ActiveDays.DaysOfMonth[13] = uiMonthDayActiveCHeckBox13.Checked;
            monitor.ActiveDays.DaysOfMonth[14] = uiMonthDayActiveCHeckBox14.Checked;
            monitor.ActiveDays.DaysOfMonth[15] = uiMonthDayActiveCHeckBox15.Checked;
            monitor.ActiveDays.DaysOfMonth[16] = uiMonthDayActiveCHeckBox16.Checked;
            monitor.ActiveDays.DaysOfMonth[17] = uiMonthDayActiveCHeckBox17.Checked;
            monitor.ActiveDays.DaysOfMonth[18] = uiMonthDayActiveCHeckBox18.Checked;
            monitor.ActiveDays.DaysOfMonth[19] = uiMonthDayActiveCHeckBox19.Checked;
            monitor.ActiveDays.DaysOfMonth[20] = uiMonthDayActiveCHeckBox20.Checked;
            monitor.ActiveDays.DaysOfMonth[21] = uiMonthDayActiveCHeckBox21.Checked;
            monitor.ActiveDays.DaysOfMonth[22] = uiMonthDayActiveCHeckBox22.Checked;
            monitor.ActiveDays.DaysOfMonth[23] = uiMonthDayActiveCHeckBox23.Checked;
            monitor.ActiveDays.DaysOfMonth[24] = uiMonthDayActiveCHeckBox24.Checked;
            monitor.ActiveDays.DaysOfMonth[25] = uiMonthDayActiveCHeckBox25.Checked;
            monitor.ActiveDays.DaysOfMonth[26] = uiMonthDayActiveCHeckBox26.Checked;
            monitor.ActiveDays.DaysOfMonth[27] = uiMonthDayActiveCHeckBox27.Checked;
            monitor.ActiveDays.DaysOfMonth[28] = uiMonthDayActiveCHeckBox28.Checked;
            monitor.ActiveDays.DaysOfMonth[29] = uiMonthDayActiveCHeckBox29.Checked;
            monitor.ActiveDays.DaysOfMonth[30] = uiMonthDayActiveCHeckBox30.Checked;
        }

        private void ClearMonitorUI()
        {
            uiMonitorsListBox.Items.Clear();

            uiMonitorNameTextBox.Text = String.Empty;
            uiMonitorDescriptionTextBox.Text = String.Empty;
            uiMonitorCreatedDateTextBox.Text = String.Empty;
            uiMonitorActiveCheckBox.Checked = false;

            uiWeekDayActiveCHeckBox0.Checked = false;
            uiWeekDayActiveCHeckBox1.Checked = false;
            uiWeekDayActiveCHeckBox2.Checked = false;
            uiWeekDayActiveCHeckBox3.Checked = false;
            uiWeekDayActiveCHeckBox4.Checked = false;
            uiWeekDayActiveCHeckBox5.Checked = false;
            uiWeekDayActiveCHeckBox6.Checked = false;

            uiMonthDayActiveCHeckBox0.Checked = false;
            uiMonthDayActiveCHeckBox1.Checked = false;
            uiMonthDayActiveCHeckBox2.Checked = false;
            uiMonthDayActiveCHeckBox3.Checked = false;
            uiMonthDayActiveCHeckBox4.Checked = false;
            uiMonthDayActiveCHeckBox5.Checked = false;
            uiMonthDayActiveCHeckBox6.Checked = false;
            uiMonthDayActiveCHeckBox7.Checked = false;
            uiMonthDayActiveCHeckBox8.Checked = false;
            uiMonthDayActiveCHeckBox9.Checked = false;
            uiMonthDayActiveCHeckBox10.Checked = false;
            uiMonthDayActiveCHeckBox11.Checked = false;
            uiMonthDayActiveCHeckBox12.Checked = false;
            uiMonthDayActiveCHeckBox13.Checked = false;
            uiMonthDayActiveCHeckBox14.Checked = false;
            uiMonthDayActiveCHeckBox15.Checked = false;
            uiMonthDayActiveCHeckBox16.Checked = false;
            uiMonthDayActiveCHeckBox17.Checked = false;
            uiMonthDayActiveCHeckBox18.Checked = false;
            uiMonthDayActiveCHeckBox19.Checked = false;
            uiMonthDayActiveCHeckBox20.Checked = false;
            uiMonthDayActiveCHeckBox21.Checked = false;
            uiMonthDayActiveCHeckBox22.Checked = false;
            uiMonthDayActiveCHeckBox23.Checked = false;
            uiMonthDayActiveCHeckBox24.Checked = false;
            uiMonthDayActiveCHeckBox25.Checked = false;
            uiMonthDayActiveCHeckBox26.Checked = false;
            uiMonthDayActiveCHeckBox27.Checked = false;
            uiMonthDayActiveCHeckBox28.Checked = false;
            uiMonthDayActiveCHeckBox29.Checked = false;
            uiMonthDayActiveCHeckBox30.Checked = false;
        }

        private void FillMonitorUI()
        {
            // monitors:
            foreach (var monitor in _suite.Monitors)
            {
                uiMonitorsListBox.Items.Add(new MonitorListItem() { ID = monitor.ID, Name = monitor.Title, Monitor = monitor });
            }
        }

        #endregion // Monitors Management

        #region Milestone Management

        public void AddMilestone()
        {
            if (_createMilesDlg.ShowDialog() == DialogResult.OK)
            {
                int id = _suite.Milestones.Add(_createMilesDlg.MilestoneDate, _createMilesDlg.MilestoneTitle, _createMilesDlg.MilestoneDescription);

                uiMilesListBox.Items.Add(new MilestoneListItem() { Milestone = _suite.Milestones[id], ID = id });
            }
        }

        public void RemoveMilestone(int id)
        {
            _suite.Milestones.Remove(id);

            for (int i = 0; i < uiMilesListBox.Items.Count; ++i)
            {
                if (((MilestoneListItem)uiMilesListBox.Items[i]).ID == id)
                {
                    uiMilesListBox.Items.Remove(uiMilesListBox.Items[i]);
                    break;
                }
            }

            // TOFIX: add count!!!

            /*/if (_suite.Milestones.Count
            {
                ClearMonitorUI();
            }
            */
        }

        private void SaveMilestone(int id)
        {
            var mile = _suite.Milestones[id];

            mile.Title = uiMileTitleTextBox.Text;
            mile.Description = uiMileDescrTextBox.Text;
        }

        private void ShowMilestoneInfo(IMilestone mile)
        {
            uiMileTitleTextBox.Text = mile.Title;
            uiMileDescrTextBox.Text = mile.Description;
            uiMileIDTextBox.Text = mile.ID.ToString();
        }

        private void ShowMilesForDate(DateTime date)
        {
            uiMilesForDateListBox.Items.Clear();

            var miles = _suite.Milestones.GetMilestones(date);

            foreach (var mile in miles)
            {
                uiMilesForDateListBox.Items.Add(new MilestoneListItem() { Milestone = mile, ID = mile.ID });
            }
        }

        private void ClearMilesUI()
        {
            uiMilesListBox.Items.Clear();
            uiMilesForDateListBox.Items.Clear();

            uiMileTitleTextBox.Text = String.Empty;
            uiMileDescrTextBox.Text = String.Empty;
        }

        private void FillMilesUI()
        {
            foreach (var mile in _suite.Milestones)
            {
                uiMilesListBox.Items.Add(new MilestoneListItem() { Milestone = mile, ID = mile.ID });
            }

            if (uiMilesListBox.Items.Count > 0)
            {
                uiMilesListBox.SelectedIndex = 0;
            }
        }

        #endregion // Milestone Management

        #region Task Management

        private void AddTask()
        {
            if (_createTaskDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int id = _suite.Tasks.Add(DateTime.Now, _createTaskDlg.TaskDeadLine, _createTaskDlg.TaskSeverity, _createTaskDlg.TaskTitle, _createTaskDlg.TaskDescription);

                uiTasksListBox.Items.Add(new TaskListItem() { Task = _suite.Tasks[id], ID = _suite.Tasks[id].ID });
            }
        }

        private void RemoveTask(int id)
        {
            _suite.Tasks.Remove(id);

            for (int i = 0; i < uiTasksListBox.Items.Count; ++i)
            {
                if ((uiTasksListBox.Items[i] as TaskListItem).ID == id)
                {
                    uiTasksListBox.Items.RemoveAt(i);
                    break;
                }
            }
        }

        private void ShowTaskList(TaskState state)
        {
            var active = _suite.Tasks.Get(state);

            uiTasksListBox.Items.Clear();
            foreach (var task in active)
            {
                uiTasksListBox.Items.Add(new TaskListItem() { Task = task, ID = task.ID });
            }
        }

        private void ShowTaskInfo(ITask task)
        {
            uiTaskCreatedDTPicker.Value = task.Created;
            uiTaskDeadLineDTPicker.Value = task.DeadLine;

            uiTaskSeverityComboBox.SelectedIndex = (int)task.Severity;
            uiTaskStateComboBox2.SelectedIndex = (int)task.State;

            uiTaskTitleTextBox.Text = task.Title;
            uiTaskDescrTextBox.Text = task.Description;

            uiTaskIDTextBox.Text = task.ID.ToString();
        }

        private void SaveTask(ITask task)
        {
            task.DeadLine = uiTaskDeadLineDTPicker.Value;

            task.Severity = (TaskSeverity)uiTaskSeverityComboBox.SelectedIndex;
            task.State = (TaskState)uiTaskStateComboBox2.SelectedIndex;

            task.Title = uiTaskTitleTextBox.Text;
            task.Description = uiTaskDescrTextBox.Text;
        }

        private void FillTasksUI()
        {
            uiTaskStateComboBox.SelectedIndex = 0;

            ShowTaskList(TaskState.Active);
        }

        private void ClearTasksUI()
        {
            uiTasksListBox.Items.Clear();

            uiTaskCreatedDTPicker.Value = DateTime.Now;
            uiTaskDeadLineDTPicker.Value = DateTime.Now;

            uiTaskSeverityComboBox.SelectedIndex = 0;
            uiTaskStateComboBox2.SelectedIndex = 0;

            uiTaskTitleTextBox.Text = String.Empty;
            uiTaskDescrTextBox.Text = String.Empty;

            uiTaskIDTextBox.Text = String.Empty;
        }

        #endregion // Task Management

        #region CountDowns Management

        private void AddCountDown()
        {
            if (_createCountDownDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int id = _suite.CountDownTimers.Add(_createCountDownDlg.CounterName, _createCountDownDlg.CounterDescription, _createCountDownDlg.CounterTargetDate, DateTime.Now);

                uiCountDownsListBox.Items.Add(new CountDownListItem() { ID = id, Timer = _suite.CountDownTimers[id] });
            }
        }

        private void RemoveCountDown(int id)
        {
            _suite.CountDownTimers.Remove(id);

            for (int i = 0; i < uiCountDownsListBox.Items.Count; ++i)
            {
                var entry = uiCountDownsListBox.Items[i] as CountDownListItem;

                if (entry != null)
                {
                    if (entry.ID == id)
                    {
                        uiCountDownsListBox.Items.RemoveAt(i);
                        break;
                    }
                }
            }
        }

        private void ShowCountDownInfo(ICountDownTimer timer)
        {
            uiCounterCreatedDateTimePicker.Value = timer.Created;
            uiCounterTargetDateTimePicker.Value = timer.TargetDate;

            uiCounterLeftTextBox.Text = (timer.TargetDate - DateTime.Now).ToString();
            uiCountDownIDTextBox.Text = timer.ID.ToString();

            uiCountDownNameTextBox.Text = timer.Title;
            uiCountDownDescrTextBox.Text = timer.Description;
        }

        private void SaveCountDown(ICountDownTimer timer)
        {
            timer.TargetDate = uiCounterTargetDateTimePicker.Value;

            timer.Title = uiCountDownNameTextBox.Text;
            timer.Description = uiCountDownDescrTextBox.Text;
        }

        private void FillCountDownsUI()
        {
            foreach (var counter in _suite.CountDownTimers)
            {
                uiCountDownsListBox.Items.Add(new CountDownListItem() { ID = counter.ID, Timer = counter });
            }

            if (uiCountDownsListBox.Items.Count > 0)
            {
                uiCountDownsListBox.SelectedIndex = 0;
            }
        }

        private void ClearCountDownsUI()
        {
            uiCountDownsListBox.Items.Clear();

            uiCounterCreatedDateTimePicker.Value = DateTime.Now;
            uiCounterTargetDateTimePicker.Value = DateTime.Now;

            uiCounterLeftTextBox.Text = String.Empty;
            uiCountDownIDTextBox.Text = String.Empty;

            uiCountDownNameTextBox.Text = String.Empty;
            uiCountDownDescrTextBox.Text = String.Empty;
        }

        #endregion // CountDowns Management

        #region MonitoringGroups Management

        public void AddMonitoringGroup()
        {
            if (_createMonitoringGroupDlg.ShowDialog(_suite) == System.Windows.Forms.DialogResult.OK)
            {
                int id = _suite.Groups.Add(_createMonitoringGroupDlg.GroupName, _createMonitoringGroupDlg.GroupDescription);

                var monitors = _createMonitoringGroupDlg.GetGroupMonitors();

                foreach (var monitorId in monitors)
                {
                    _suite.Groups[id].Monitors.Add(_suite.Monitors[monitorId]);
                }

                var tasks = _createMonitoringGroupDlg.GetGroupTasks();

                foreach (var taskId in tasks)
                {
                    _suite.Groups[id].Tasks.Add(_suite.Tasks[taskId]);
                }

                _suite.Groups[id].Changed += OnMonitoringGroupChanged;
                uiMonitoringGroupsListBox.Items.Add(new MonitoringGroupListItem() { Group = _suite.Groups[id], ID = id });
            }
        }

        public void RemoveMonitoringGroup(int id)
        {
            var group = _suite.Groups[id];

            uiMonitoringGroupsListBox.Items.Remove(group);
            _suite.Groups.Remove(id);
        }

        public void ShowMonitoringGroupInfo(IMonitoringGroup group)
        {
            uiMonitoringGroupNameTextBox.Text = group.Title;
            uiMonitoringGroupDescrTextBox.Text = group.Description;

            // fill added monitors
            uiMonitoringGroupAddedMonitorsListBox.Items.Clear();

            foreach (var monitor in group.Monitors)
            {
                uiMonitoringGroupAddedMonitorsListBox.Items.Add(new MonitorListItem() { Monitor = monitor, Name = monitor.Title, ID = monitor.ID });
            }

            // fill available monitors
            uiMonitoringGroupAvailableMonitorsListBox.Items.Clear();

            foreach(var monitor in _suite.Monitors)
            {
                if (!group.Monitors.Contains(monitor.ID))
                {
                    uiMonitoringGroupAvailableMonitorsListBox.Items.Add(new MonitorListItem() { Monitor = monitor, Name = monitor.Title, ID = monitor.ID });
                }
            }

            // fill added tasks
            uiMonitoringGroupAddedTasksListBox.Items.Clear();

            foreach (var task in group.Tasks)
            {
                uiMonitoringGroupAddedTasksListBox.Items.Add(new TaskListItem() { Task = task, ID = task.ID });
            }

            // fill available tasks
            uiMonitoringGroupAvailableTasksListBox.Items.Clear();

            foreach (var task in _suite.Tasks)
            {
                if (!group.Tasks.Contains(task.ID))
                {
                    uiMonitoringGroupAvailableTasksListBox.Items.Add(new TaskListItem() { Task = task, ID = task.ID });
                }
            }
        }

        public void SaveMonitoringGroup(IMonitoringGroup group)
        {
            group.Title = uiMonitoringGroupNameTextBox.Text;
            group.Description = uiMonitoringGroupDescrTextBox.Text;

            // remove removed monitors
            foreach (MonitorListItem monitor in uiMonitoringGroupAvailableMonitorsListBox.Items)
            {
                if (group.Monitors.Contains(monitor.ID))
                {
                    group.Monitors.Remove(monitor.ID);
                }
            }

            // add added monitors
            foreach (MonitorListItem monitor in uiMonitoringGroupAddedMonitorsListBox.Items)
            {
                if (!group.Monitors.Contains(monitor.ID))
                {
                    group.Monitors.Add(monitor.Monitor);
                }
            }

            // remove removed tasks
            foreach (TaskListItem task in uiMonitoringGroupAvailableTasksListBox.Items)
            {
                if (group.Tasks.Contains(task.ID))
                {
                    group.Tasks.Remove(task.ID);
                }
            }

            // add added tasks
            foreach (TaskListItem task in uiMonitoringGroupAddedTasksListBox.Items)
            {
                if (!group.Tasks.Contains(task.ID))
                {
                    group.Tasks.Add(task.Task);
                }
            }
        }

        private void FillMonitoringGroupsUI()
        {
            foreach (var group in _suite.Groups)
            {
                group.Changed += OnMonitoringGroupChanged;
                uiMonitoringGroupsListBox.Items.Add(new MonitoringGroupListItem() { Group = group, ID = group.ID });
            }
        }

        private void ClearMonitoringGroupsUI()
        {
            uiMonitoringGroupsListBox.Items.Clear();

            uiMonitoringGroupNameTextBox.Text = String.Empty;
            uiMonitoringGroupDescrTextBox.Text = String.Empty;

            uiMonitoringGroupAvailableMonitorsListBox.Items.Clear();
            uiMonitoringGroupAddedMonitorsListBox.Items.Clear();

            uiMonitoringGroupAvailableTasksListBox.Items.Clear();
            uiMonitoringGroupAddedTasksListBox.Items.Clear();
        }

        private void OnMonitoringGroupChanged(IMonitoringGroup sender, MonitoringGroupChangeKind kind)
        {
            var selected = uiMonitoringGroupsListBox.SelectedItem as MonitoringGroupListItem;

            if (selected != null && selected.ID == sender.ID)
            {
                /*
                switch (kind)
                {
                    case MonitoringGroupChangeKind.MonitorDeleted:
                        //
                        break;

                    case MonitoringGroupChangeKind.TaskDeleted:
                        //
                        break;
                }
                */

                ShowMonitoringGroupInfo(selected.Group);
            }
        }

        #endregion // MonitoringGroups Management

        #region Buys Management

        private void AddBuy()
        {
            if (_createBuyDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int id = _suite.Buys.Add(_createBuyDlg.BuyTitle, _createBuyDlg.BuyDescription, _createBuyDlg.BuySeverity, _createBuyDlg.BuyPrice, DateTime.Now, _createBuyDlg.BuyDeadLine);

                uiBuyListBox.Items.Add(new BuyListItem() { Buy = _suite.Buys[id] });
            }
        }

        private void RemoveBuy(int id)
        {
            _suite.Buys.Remove(id);

            for (int i = 0; i < uiBuyListBox.Items.Count; ++i)
            {
                var buy = uiBuyListBox.Items[i] as BuyListItem;

                if (buy != null && buy.Buy.ID == id)
                {
                    uiBuyListBox.Items.RemoveAt(i);
                    break;
                }
            }
        }

        private void SaveBuy(IBuy buy)
        {
            buy.Title = uiBuyTitleTextBox.Text;
            buy.Description = uiBuyDescrTextBox.Text;

            buy.State = (BuyState)uiBuyStateDropDown.SelectedIndex;
            buy.Severity = (int)uiBuySeverityUpDown.Value;
            buy.Price = (double)uiBuyPriceUpDown.Value;

            // buy.Created = uiBuyCreatedCalendar.SelectionStart;
            buy.DeadLine = uiBuyDeadLineCalendar.SelectionStart;
        }

        private void ShowBuyInfo(IBuy buy)
        {
            uiBuyTitleTextBox.Text = buy.Title;
            uiBuyDescrTextBox.Text = buy.Description;

            uiBuyStateDropDown.SelectedIndex = (int)buy.State;
            uiBuySeverityUpDown.Value = buy.Severity;
            uiBuyPriceUpDown.Value = (decimal)buy.Price;

            uiBuyCreatedCalendar.SelectionStart = buy.Created;
            uiBuyCreatedCalendar.SelectionEnd = buy.Created;

            uiBuyDeadLineCalendar.SelectionStart = buy.DeadLine;
            uiBuyDeadLineCalendar.SelectionEnd = buy.DeadLine;

            uiBuyIDTextBox.Text = buy.ID.ToString();
        }

        private void ClearBuysUI()
        {
            uiBuyListBox.Items.Clear();

            uiBuyTitleTextBox.Text = String.Empty;
            uiBuyDescrTextBox.Text = String.Empty;

            uiBuyStateDropDown.SelectedIndex = 0;
            uiBuySeverityUpDown.Value = 0;
            uiBuyPriceUpDown.Value = 0;

            uiBuyCreatedCalendar.SelectionStart = DateTime.Now;
            uiBuyDeadLineCalendar.SelectionStart = DateTime.Now;

            uiBuyIDTextBox.Text = String.Empty;
        }

        private void FillBuysUI()
        {
            uiBuyListBox.Items.Clear();

            foreach (var buy in _suite.Buys)
            {
                uiBuyListBox.Items.Add(new BuyListItem() { Buy = buy });
            }

            if (uiBuyListBox.Items.Count > 0)
            {
                uiBuyListBox.SelectedIndex = 0;
            }
        }

        #endregion // Buys Management

        #region Notes Management

        private void AddNote()
        {
            if (_createNoteDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int id = _suite.Notes.Add(_createNoteDlg.NoteTitle, _createNoteDlg.NoteText);

                uiNotesListBox.Items.Add(new NoteListItem() { Note = _suite.Notes[id] });
            }
        }

        private void RemoveNote(int id)
        {
            _suite.Notes.Remove(id);

            for (int i = 0; i < uiNotesListBox.Items.Count; ++i)
            {
                var entry = uiNotesListBox.Items[i] as NoteListItem;

                if (entry != null && entry.Note.ID == id)
                {
                    uiNotesListBox.Items.RemoveAt(i);
                    break;
                }
            }
        }

        private void SaveNote(INote note)
        {
            note.Title = uiNoteTitleTextBox.Text;
            note.Text = uiNoteTextTextBox.Text;
        }

        private void ShowNoteInfo(INote note)
        {
            uiNoteTitleTextBox.Text = note.Title;
            uiNoteTextTextBox.Text = note.Text;
            uiNoteCreatedCalendar.SelectionStart = note.Created;
            uiNoteCreatedCalendar.SelectionEnd = note.Created;
            uiNoteIDTextBox.Text = note.ID.ToString();
        }

        private void ClearNotesUI()
        {
            uiNotesListBox.Items.Clear();

            uiNoteTitleTextBox.Text = String.Empty;
            uiNoteTextTextBox.Text = String.Empty;
            uiNoteCreatedCalendar.SelectionStart = DateTime.Now;
            uiNoteCreatedCalendar.SelectionEnd = DateTime.Now;
            uiNoteIDTextBox.Text = String.Empty;
        }

        private void FillNotesUI()
        {
            uiNotesListBox.Items.Clear();

            foreach (var note in _suite.Notes)
            {
                uiNotesListBox.Items.Add(new NoteListItem() { Note = note });
            }

            if (uiNotesListBox.Items.Count > 0)
            {
                uiNotesListBox.SelectedIndex = 0;
            }
        }

        #endregion // Notes Management

        private void UpdateUI()
        {
            ClearUI();

            if (_suite != null)
            {
                FillUI();
            }
        }

        private void ClearUI()
        {
            ClearMonitorUI();
            ClearSummaryUI();
            ClearMilesUI();
            ClearTasksUI();
            ClearCountDownsUI();
            ClearMonitoringGroupsUI();
            ClearBuysUI();
            ClearNotesUI();
        }

        private void FillUI()
        {
            FillMonitorUI();
            FillSummaryUI();
            FillMilesUI();
            FillTasksUI();
            FillCountDownsUI();
            FillMonitoringGroupsUI();
            FillBuysUI();
            FillNotesUI();
        }
    }
}